
/*
 * @Author: zhangxiao
 * @Email: zhangxiao0125@163.com
 * @Date: 2020-05-28 16:29:28
 * @Last Modified by: zhangxiao
 * @Last Modified time: 2020-06-09 14:40:27
 * @Description: 登录向导
 */

import 'package:flutter/material.dart';
import 'package:flutter_code_framework/base/base_main_tabbar.dart';
import 'package:flutter_code_framework/base/base_widget.dart';
import 'package:flutter_code_framework/model_data/user/data.dart';
import 'package:flutter_code_framework/route/route_transition.dart';
import 'package:flutter_code_framework/utils/global.dart';
import 'package:flutter_sfsymbols/flutter_sfsymbols.dart';
class UserGuide extends BaseWidget {
  UserGuide({Key key}) : super(key: key);

  @override
  BaseWidgetState<BaseWidget> getState() {
    return _UserGuideState();
  }
} 

class _UserGuideState extends BaseWidgetState<UserGuide> {

  FixedExtentScrollController periodScrollController =FixedExtentScrollController(initialItem: 1);
  Map<String, Object> periodPickerValue = { "id":-1, "name":"请选择"};
  List<dynamic> periodList = [];
  setPeriodPickerValue(int index) {
    setState(() {
      periodPickerValue = periodList[index];
    });
  }

//年级 Picker
  FixedExtentScrollController gradeScrollController =FixedExtentScrollController(initialItem: 0);
  Map<String, Object> gradePickerValue = {"id": -1, "name": "请选择"};
  List<dynamic> gradeList = [];
  setGradePickerValue(int index) {
    setState(() {
      gradePickerValue = gradeList[index];
      gradeScrollController = FixedExtentScrollController(initialItem: index);
    });
  }

  //学科 Picker
  FixedExtentScrollController subjectScrollController = FixedExtentScrollController(initialItem: 0);
  Map<String, Object> subjectPickerValue = {"id": -1, "name": "请选择"};
  List<dynamic> subjectList = [];
  setSubjectPickerValue(int index) {
    setState(() {
      subjectPickerValue = subjectList[index];
      subjectScrollController = FixedExtentScrollController(initialItem: index);
    });
  }

//职称 Picker
   FixedExtentScrollController jobScrollController =FixedExtentScrollController(initialItem: 0);
  Map<String, Object> jobPickerValue = {"id": -1, "name": "请选择"};
  List<dynamic> jobList = [];
  setJobPickerValue(int index) {
    setState(() {
      jobPickerValue = jobList[index];
      jobScrollController = FixedExtentScrollController(initialItem: index);
    });
  }

  @override
  Widget buildWidget(BuildContext context) {
    getGradeWiget(String _lable, Widget _picker) {
      return  Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.fromLTRB(24, 16, 0, 0),
                    child: Text(
                      _lable,
                      style: TextStyle(
                          color: Theme.of(context).primaryTextTheme.caption.color,
                          fontSize: 18)
                    )
                  )
                ]
              ),
              Row(
                children: <Widget>[
                  Container(
                    child:Padding(
                    padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                    child: _picker
                  ),
                  )
                ]
              ),
              Padding(
                padding: EdgeInsets.only(left:24.0,right:24.0),
                child: Divider(
                height: 1,
                color: Theme.of(context).primaryTextTheme.caption.color,
              )
              )
            ],
          );
    }
   
    //授课学段
    Widget periodInfo =Container(
      color: Colors.transparent,
      margin: EdgeInsets.only(left:24.0,top:14.0),
      width: MediaQuery.of(context).size.width - 48,
      height: 30,
      child:Text(periodPickerValue["name"], style: TextStyle( color: Theme.of(context).primaryTextTheme.caption.color, fontSize: 14)),
    );
    //授课年级
    Widget gradeInfo =Container(
         color: Colors.transparent,
         margin: EdgeInsets.only(left:24.0,top:14.0),
         width: MediaQuery.of(context).size.width - 48,
         height: 30,
        child:Text(gradePickerValue["name"], style: TextStyle( color: Theme.of(context).primaryTextTheme.caption.color, fontSize: 14)) ,
      );
    
    //授课年级
    Widget subjectInfo =Container(
         color: Colors.transparent,
         margin: EdgeInsets.only(left:24.0,top:14.0),
         width: MediaQuery.of(context).size.width - 48,
         height: 30,
         child:Text(subjectPickerValue["name"],style: TextStyle(color: Theme.of(context).primaryTextTheme.caption.color,fontSize: 14))
    );
    
    
    //职称
    Widget jobInfo =Container(
         color: Colors.transparent,
         margin: EdgeInsets.only(left:24.0,top:14.0),
         width: MediaQuery.of(context).size.width - 48,
         height: 30,
        child: Text(jobPickerValue["name"],style: TextStyle(color: Theme.of(context).primaryTextTheme.caption.color,fontSize: 14))
      );

    return SingleChildScrollView(child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(left:24,top:26.0),
              child: Text('教学信息',style: TextStyle(color: Color.fromRGBO(72,72,72,1),fontSize: 28.0)),
            ),
            Container(
              margin: EdgeInsets.only(top:26.0),
              child: Icon(SFSymbols.flag_circle_fill,color: Color.fromRGBO(57,135,110,1))),
          ]),
         Container(
           margin: EdgeInsets.only(left:24.0,top:16.0),
           child: Text('请根据实际情况选择您的授课信息',style: TextStyle(color: Color.fromRGBO(72,72,72,0.7),fontSize: 16.0)),
         ),
         Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                   SizedBox(height: 26), //空出顶部位置
                   getGradeWiget('授课学段',showPickerList(context, periodScrollController, periodList,periodInfo, setPeriodPickerValue)),
                   getGradeWiget('授课年级',showPickerList(context, gradeScrollController, gradeList, gradeInfo, setGradePickerValue)),
                   getGradeWiget('授课学科',showPickerList(context, subjectScrollController, subjectList, subjectInfo, setSubjectPickerValue)),
                   getGradeWiget('职称',showPickerList(context, jobScrollController, jobList, jobInfo,setJobPickerValue)),
                   Container(
                        margin: EdgeInsets.only(left:24.0,top:40.0,right: 24.0),
                        child: GestureDetector(
                          child: Container(
                            height: 46,
                            width: double.infinity,
                            decoration: BoxDecoration(
                                color: Theme.of(context).primaryColor,
                                borderRadius: BorderRadius.circular(5.0)),
                            child: Center(
                              child: Text(
                                '完    成',
                                style: TextStyle(
                                    color: Theme.of(context).primaryTextTheme.button.color,
                                    fontSize: 16),
                              ),
                            ),
                          ),
                          onTap:guide 
                        )
                      )
                ] )
       ]));
  }
  
  //向导信息维护
  guide() async{
    int _subjectId=subjectPickerValue["id"];
    int _periodId=periodPickerValue["id"];
    int _gradeId=gradePickerValue["id"];
    String _jobTitle=jobPickerValue["id"]==-1?'':jobPickerValue["name"];
    var response=await G.req.userGuide.guide(subjectId: _subjectId,periodId: _periodId,gradeId: _gradeId,jobTitle: _jobTitle,isEdit:false);

    if(response.data['isOK']==false){
        showRightToast(response.data['errMsg']);
    }else{
      RouteTransition(BaseMainTabbarPage()).pushAndRemoveUntilTransRoute(context);
    }
  }
  

  initPeroidDataList() async {
    //学段
    var _period = await G.req.periodData.getList();
    if (_period.data['periodList'].length > 0) {
      setState(() {
        periodList = _period.data['periodList'];
      });
    }
    
        //职称
    var _jobTitle = await G.req.common.getJobTitlelist();
    if (_jobTitle.data['isOK'] != false) {
      var tepJob = [];
      _jobTitle.data['list'].forEach((element) {
        tepJob.add({"id": element["Id"], "name": element['Name']});
      });
      setState(() {
        jobList = tepJob;
      });
    }
     
  }

  refrehBaseList() async {
    //年级
    var _grade = await G.req.gradeData
        .getList(periodId: periodPickerValue["id"].toString());
    if (_grade.data['gradeList'].length > 0) {
      setState(() {
        gradeList = _grade.data['gradeList'];
      });
    }

    //学科
    var _subject = await G.req.subjectData.getList(
        schoolId: G.user.data.schoolId.toString(),
        periodId: periodPickerValue['id'].toString());
    if (_subject.data['subjectList'].length > 0) {
      setState(() {
        subjectList = _subject.data['subjectList'];
      });
    }
  }

  initBaseData() async {
    await initPeroidDataList();
    //加载数据
    if (periodList != null && periodList.length > 0) {
      setState(() {
        periodPickerValue = periodList.firstWhere(
            (element) => element['id'] == G.user.data.periodId, orElse: () {
          return periodList[0];
        });
        periodScrollController = FixedExtentScrollController(
            initialItem: periodList.indexOf(periodPickerValue));
      });
    }
    await refrehBaseList();
    //加载数据
    if (subjectList != null && subjectList.length > 0) {
      setState(() {
        subjectPickerValue = subjectList.firstWhere(
            (element) => element['id'] == G.user.data.subjectId, orElse: () {
          return subjectList[0];
        });
        subjectScrollController = FixedExtentScrollController(
            initialItem: subjectList.indexOf(subjectPickerValue));
      });
    }
    if (gradeList != null && gradeList.length > 0) {
      setState(() {
        gradePickerValue = gradeList.firstWhere(
            (element) => element['id'] == G.user.data.gradeId, orElse: () {
          return gradeList[0];
        });
        gradeScrollController = FixedExtentScrollController(
            initialItem: gradeList.indexOf(gradePickerValue));
      });
    }
  }

  @override
  void initState() {
    super.initState();
       //初始化基础数据
    initBaseData();
  }

  @override
  void onCreate() {
     setAppTopNavBarVisible(true);
    // log("onCreate");
  }

  @override
  void onPause() {
    // log('onPause');
  }

  @override
  void onResume() {
    // log("onResume");
  }
 
}
